package com.edu.service.impl;

import com.edu.entity.Product;
import com.edu.repository.ProductRepository;
import com.edu.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    /**
     * 查询单个产品
     *
     * @param id 产品编号
     * @return 产品 或 null
     */

    @Override
    public Product findOne(Long id) {
        log.debug("查询单个产品，id={}", id);

        Product result = productRepository.findOne(id);

        log.debug("查询单个产品，返回结果={}", result);
        return result;
    }

    /**
     * 创建产品
     *
     * @param product 要添加的产品对象
     * @return
     */
    @Override
    public Product addProduct(Product product) {

        log.debug("创建产品，参数={}", product);

        Product result = productRepository.save(product);

        log.debug("创建产品，返回结果={}", result);

        return result;
    }

    /**
     * 复杂查询
     * @param ids 编号列表
     * @param minRewardRate 最小收益率
     * @param maxRewardRate 最小收益率
     * @param status 状态列表
     * @param pageable 分页对象
     * @return
     */
    @Override
    public Page<Product> query(List<Long> ids, BigDecimal minRewardRate, BigDecimal maxRewardRate, List<String> status, Pageable pageable) {


        log.debug("查询产品,ids={}, minRewardRate={}, maxRewardRate={}, status={}, pageable={} ",ids, minRewardRate, maxRewardRate, status, pageable);

        Specification<Product> specification = (root, criteriaQuery, criteriaBuilder) -> {

            Expression<Long> idCol = root.get("id");
            Expression<BigDecimal> rewardRateCol = root.get("rewardRate");
            Expression<String> statusCol = root.get("status");
            List<Predicate> predicates = new ArrayList<>();
            if (!CollectionUtils.isEmpty(ids)){
                predicates.add(idCol.in(ids));
            }
            /**
             * BigDecimal.ZERO.compareTo(bigdemical)  = -1,表示bigdemical大于0；
             * BigDecimal.ZERO.compareTo(bigdemical)  =  0,表示bigdemical等于0；
             * BigDecimal.ZERO.compareTo(bigdemical)  =  1,表示bigdemical小于0；
             */
            if (minRewardRate != null && BigDecimal.ZERO.compareTo(minRewardRate) < 0) {
                predicates.add(criteriaBuilder.ge(rewardRateCol, minRewardRate));
            }
            if (minRewardRate != null && BigDecimal.ZERO.compareTo(minRewardRate) < 0) {
                predicates.add(criteriaBuilder.le(rewardRateCol, maxRewardRate));
            }

            if (!CollectionUtils.isEmpty(status)){
                predicates.add(statusCol.in(status));
            }
            return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        };
        Page<Product> products = productRepository.findAll(specification, pageable);

        log.debug("查询产品, 返回值={} ", products);

        return products;
    }

}
